//
//  BankZhiViewController.m
//  51易借
//
//  Created by apple on 2017/9/20.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "BankZhiViewController.h"
#import "ZhiBankTableViewCell.h"
#import "BankZhiHeaderView.h"
#import "BankModel.h"
@interface BankZhiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSArray * dataArr;
@end

@implementation BankZhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupNavBar];
    [self setupUI];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getZhiBanks];
}



- (void)setupNavBar{
    
    self.title = @"支持银行";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}
- (void)setupUI{

    self.view.backgroundColor = BACK_COLOR;
    self.topView.backgroundColor = TOTAL_COLOR;
    
    self.tableView.backgroundColor = BACK_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = YES;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZhiBankTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"item"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*SCREENH6;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZhiBankTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"item" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BankModel * model = self.dataArr[indexPath.row];
    cell.nameLabel.text = model.bankName;
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    BankZhiHeaderView * view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([BankZhiHeaderView class]) owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50*SCREENH6);
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SCREENH6*50;
}


-(void)getZhiBanks
{
    ParaModel*para = [ParaModel new];
    
    
    [[NetworkTool shareManager] POST:[Interfacefile loadSupportBankList] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            self.dataArr = [BankModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil];
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
        [self.tableView reloadData];
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
}


- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
